package com.controller;

import com.dto.BaseDto;
import com.entity.Student;
import com.page.PageInfo;
import com.servcice.StudentService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.Option;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Controller
public class StudentController extends BaseController{
    @Resource
    private StudentService studentService;

    @RequiresRoles({"teacher","student"})
    @RequestMapping("/student/getAllJSon")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Student> getAllJSon() throws Exception{
        return studentService.getAll();
    }

    /**
     * 前台查询分页显示，ajax方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/student/page")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Student> getStudentsByPage(PageInfo pageInfo) throws Exception{

            String cacheId =  "studentsByPage";
        return studentService.getStudentsByPage(pageInfo,cacheId);
    }

    /**
     * 前台 根据姓名搜索学生
     * @param name
     * @return
     */
        @RequestMapping("/student/searchStudentByName/{name}")
        @ResponseBody //将返回值以json格式输出
        //@PathVariable 将url中以{xxx}形式传递的参数值 绑定给当前的方法某一个参数，就可以解决传递的参数名和方法定义的参数名 名称不同无法自动绑定的问题
        public BaseDto<Student> searchStudentByName(@PathVariable("name") String name, HttpServletRequest request) throws Exception{
            String currPage = request.getParameter("currPage");
            String pageSize = request.getParameter("pageSize");
            PageInfo pageInfo = new PageInfo();
            currPage = Optional.ofNullable(currPage).orElse(String.valueOf(pageInfo.getCurrPage()));
            pageSize = Optional.ofNullable(pageSize).orElse(String.valueOf(pageInfo.getPageSize()));

            pageInfo.setCurrPage(Integer.parseInt(currPage));
            pageInfo.setPageSize(Integer.parseInt(pageSize));
            return studentService.searchStudentByName("%"+name+"%",pageInfo);
        }
    /**
     * 后台查询分页显示，跳转方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/student/back/page")
    public ModelAndView getStudentsBackByPage(PageInfo pageInfo) throws Exception{
        String cacheId =   "studentsByPage"+pageInfo.getCurrPage();//将页码拼接上，所以每一页都在redis有一个缓存
        BaseDto<Student> studentsDTO = studentService.getStudentsByPage(pageInfo,cacheId);
        return new ModelAndView("/back/student/list","dto",studentsDTO);
    }



    /**
     * 前台根据学号（主键查询学生带选课信息）
     * @param sno
     * @return
     * @throws Exception
     */
    //restful风格  路径不能有动词  /student/back/student 表示按主键查询
    //                         /student/back/students  表示查询全部
    //                         /student/back/pageStudents 表示分页查询
    //                         /student/back/
    @RequestMapping(value = "/front/studentInfo/{sno}")
    public ModelAndView selectStudentWithScsAndCourse(@PathVariable("sno") String sno) throws Exception{
        System.out.println("2222          "+sno);
        BaseDto<Student> studentsDTO = studentService.selectStudentWithScsAndCourse(sno);
        return new ModelAndView("/front/student/studentInfo","dto",studentsDTO);
    }

    @RequestMapping(value = "/front/studentInfoJson/{sno}")
    @ResponseBody
    public BaseDto selectStudentWithScsAndCourse1(@PathVariable("sno") String sno) throws Exception{

        BaseDto<Student> studentsDTO = studentService.selectStudentWithScsAndCourse(sno);
        return studentsDTO;
    }

    /**
     * 学生转班
     * @param sno
     * @param newCno
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/front/studentChangeClazz")
    @ResponseBody
    public BaseDto  studentChangeClazz(String sno,String newCno)throws Exception{
      return studentService.studentChangeClazz(sno,newCno);
    }



    @RequestMapping("/student/getAll")
    public ModelAndView getAll(){
        return new ModelAndView("/student/getAll");
    }

    @RequestMapping("/student/add")
    @ResponseBody
    public int addStudent(Student student) throws Exception{
        return studentService.addStudent(student);
    }
}
